package com.weir.workdemo.work01.future.forkjoin;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author weir
 */
@SuppressWarnings("AlibabaThreadPoolCreation")
@Slf4j
public class MyTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        List<Future> futures = new ArrayList<>();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("主线程耗时统计");
        log.info("主线程开始派活！");
        for (int i = 0; i < 10; i++) {
            Future<Void> future = executorService.submit(new MyTestTask(i+1));
            futures.add(future);
        }

        for (Future future : futures) {
            future.get();
        }
        stopWatch.stop();

        log.info("主线程执行完成！{}s", stopWatch.getTotalTimeSeconds());
        log.info(stopWatch.prettyPrint());
        executorService.shutdown();
    }

}


